﻿/**
 * Create in 2012-1-26.
 */
package kit4j.ui.cs.awt.image;

import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;

import kit4j.ui.cs.awt.image.drawer.IDrawer;
import kit4j.ui.cs.awt.image.drawer.IStringDrawer;
import kit4j.ui.cs.awt.image.drawer.RandomColorFillDrawer;
import kit4j.ui.cs.awt.image.drawer.RandomColorPointDrawer;
import kit4j.ui.cs.awt.image.drawer.RandomStyleStringDrawer;

/**
 * 一个简单实现的验证图像生成器。提供了
 * {@link kit4j.ui.cs.awt.image.IVerificationImageGenerator IVerificationImageGenerator}
 * 接口的基本实现，可通过对backgroundDrawer、codeDrawer、foregroundDrawer进行配置更改生成的图像样式。
 * 
 * @author MiniKnife
 * 
 */
public class SimpleVerificationImageGenerator implements
		IVerificationImageGenerator {
	
	private static int DEFAULT_IMAGE_WIDTH = 150;
	
	private static int DEFAULT_IMAGE_HEIGHT = 50;

	/**
	 * 生成的图像的宽度。
	 */
	private int imageWidth;

	/**
	 * 生成的图像的高度。
	 */
	private int imageHeight;

	/**
	 * 生成的图像的图像类型。
	 * 
	 * @see {@link java.awt.image.BufferedImage#TYPE_INT_RGB TYPE_INT_RGB},
	 *      {@link java.awt.image.BufferedImage#TYPE_INT_ARGB TYPE_INT_ARGB},
	 *      {@link java.awt.image.BufferedImage#TYPE_INT_ARGB_PRE TYPE_INT_ARGB_PRE},
	 *      {@link java.awt.image.BufferedImage#TYPE_INT_BGR TYPE_INT_BGR},
	 *      {@link java.awt.image.BufferedImage#TYPE_3BYTE_BGR TYPE_3BYTE_BGR},
	 *      {@link java.awt.image.BufferedImage#TYPE_4BYTE_ABGR TYPE_4BYTE_ABGR}, 
	 *      {@link java.awt.image.BufferedImage#TYPE_4BYTE_ABGR_PRE TYPE_4BYTE_ABGR_PRE},
	 *      {@link java.awt.image.BufferedImage#TYPE_BYTE_GRAY TYPE_BYTE_GRAY},
	 *      {@link java.awt.image.BufferedImage#TYPE_USHORT_GRAY TYPE_USHORT_GRAY},
	 *      {@link java.awt.image.BufferedImage#TYPE_BYTE_BINARY TYPE_BYTE_BINARY},
	 *      {@link java.awt.image.BufferedImage#TYPE_BYTE_INDEXED TYPE_BYTE_INDEXED},
	 *      {@link java.awt.image.BufferedImage#TYPE_USHORT_565_RGB TYPE_USHORT_565_RGB},
	 *      {@link java.awt.image.BufferedImage#TYPE_USHORT_555_RGB TYPE_USHORT_555_RGB}
	 */
	private int imageType;

	private IDrawer backgroundDrawer;

	private IStringDrawer codeDrawer;

	private IDrawer foregroundDrawer;

	/**
	 * 创建一个新的验证图像生成器。<br />
	 * 默认的图像宽为150，高为50，图像类型为{@link java.awt.image.BufferedImage#TYPE_INT_RGB TYPE_INT_RGB}，
	 * backgroundDrawer为{@link kit4j.ui.cs.awt.image.drawer.RandomColorFillDrawer RandomFillColorDrawer}，
	 * codeDrawer为{@link kit4j.ui.cs.awt.image.drawer.RandomStyleStringDrawer RandomStyleStringDrawer}，
	 * foregroundDrawer为{@link kit4j.ui.cs.awt.image.drawer.RandomColorPointDrawer RandomColorDotDrawer}。
	 */
	public SimpleVerificationImageGenerator() {
		imageWidth = DEFAULT_IMAGE_WIDTH;
		imageHeight = DEFAULT_IMAGE_HEIGHT;
		imageType = BufferedImage.TYPE_INT_RGB;
		
		backgroundDrawer = new RandomColorFillDrawer();
		codeDrawer = new RandomStyleStringDrawer();
		foregroundDrawer = new RandomColorPointDrawer();
	}

	/**
	 * 根据验证码生成一个验证图像。
	 * 
	 * @param key
	 *            验证码
	 * @return 生成的验证图像
	 */
	@Override
	public Image next(String verificationCode) {
		Image verificationImage = createImage();

		verificationImage = backgroundDrawer.draw(verificationImage);
		verificationImage = codeDrawer.draw(verificationImage, verificationCode);
		verificationImage = foregroundDrawer.draw(verificationImage);

		return verificationImage;
	}

	/**
	 * 根据配置的参数创建图像。
	 * 
	 * @return 创建的图像。
	 * @see {@link java.awt.image.BufferedImage#BufferedImage(int, int, int) BufferedImage(int, int, int)}
	 */
	protected BufferedImage createImage() {
		return new BufferedImage(imageWidth, imageHeight, imageType);
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#getImageWidth()
	 */
	@Override
	public int getImageWidth() {
		return this.imageWidth;
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#setImageWidth(int)
	 */
	@Override
	public void setImageWidth(int width) {
		this.imageWidth = width;
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#getImageHeight()
	 */
	@Override
	public int getImageHeight() {
		return this.imageHeight;
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#setImageHeight(int)
	 */
	@Override
	public void setImageHeight(int height) {
		this.imageHeight = height;
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#getImageType()
	 */
	@Override
	public int getImageType() {
		return this.imageType;
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#setImageType(int)
	 */
	@Override
	public void setImageType(int imageType) {
		this.imageType = imageType;
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#getMinFontSize()
	 */
	@Override
	public float getMinFontSize() {
		return codeDrawer.getMinFontSize();
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#setMinFontSize(float)
	 */
	@Override
	public void setMinFontSize(float fontSize) {
		codeDrawer.setMinFontSize(fontSize);
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#getMaxFontSize()
	 */
	@Override
	public float getMaxFontSize() {
		return codeDrawer.getMaxFontSize();
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#setMaxFontSize(float)
	 */
	@Override
	public void setMaxFontSize(float fontSize) {
		codeDrawer.setMaxFontSize(fontSize);
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#getVerificationCodeMargin()
	 */
	@Override
	public Insets getVerificationCodeMargin() {
		return codeDrawer.getMargin();
	}

	/**
	 * @see kit4j.ui.cs.awt.image.IVerificationImageGenerator#setVerificationCodeMargin(java.awt.Insets)
	 */
	@Override
	public void setVerificationCodeMargin(Insets margin) {
		codeDrawer.setMargin(margin);
	}

	public IDrawer getBackgroundDrawer() {
		return backgroundDrawer;
	}

	/**
	 * 设置图像的背景绘制器。
	 * 
	 * @param backgroundDrawer
	 *            背景绘制器
	 */
	public void setBackgroundDrawer(IDrawer backgroundDrawer) {
		this.backgroundDrawer = backgroundDrawer;
	}

	public IStringDrawer getCodeDrawer() {
		return codeDrawer;
	}

	/**
	 * 设置图像的验证码绘制器。
	 * 
	 * @param codeDrawer
	 *            验证码绘制器
	 */
	public void setCodeDrawer(IStringDrawer codeDrawer) {
		this.codeDrawer = codeDrawer;
	}

	public IDrawer getForegroundDrawer() {
		return foregroundDrawer;
	}

	/**
	 * 设置图像的前景绘制器。
	 * 
	 * @param foregroundDrawer
	 *            前景绘制器
	 */
	public void setForegroundDrawer(IDrawer foregroundDrawer) {
		this.foregroundDrawer = foregroundDrawer;
	}

}
